package com.sh169.beans;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.sh169.util.urlmatch.AntPathMatcher;

public class ApplicationConfig {

	private static String index_uri = "/";
	private static String login_uri = "/login.jsp";
	private static String check_uri = "/check.do";
	private static String logout_uri = "/logout.do";
	private static String page_403 = "/error/403.jsp";
	private static String page_404 = "/error/404.jsp";
	private static String page_500 = "/error/500.jsp";
	
	private static List<RoleUrl> roleUrls = null;
	private static AntPathMatcher matcher = new AntPathMatcher();
	public static List<String> getRoles4Url(String url){
		if(roleUrls==null) return null;
		for(RoleUrl ru: roleUrls){
			if(matcher.match(ru.getUrl(), url)){
				return ru.getAccess();
			}
		}
		return null;
	}
	
	public static String getPage_403() {
		return page_403;
	}
	public static String getPage_404() {
		return page_404;
	}
	public static String getPage_500() {
		return page_500;
	}
	public static String getIndex_uri() {
		return index_uri;
	}
	public static String getLogin_uri() {
		return login_uri;
	}
	public static String getCheck_uri() {
		return check_uri;
	}
	public static String getLogout_uri() {
		return logout_uri;
	}
	public static List<RoleUrl> getRoleUrls() {
		return roleUrls;
	}
	@SuppressWarnings("unchecked")
	public static void setConfig(Map<String, Object> config) throws Exception{
		if(config.get("index") != null){
			index_uri = (String) config.get("index");
		}
		if(config.get("login") != null){
			login_uri = (String) config.get("login");
		}
		if(config.get("logout") != null){
			logout_uri = (String) config.get("logout");
		}
		if(config.get("check") != null){
			check_uri = (String) config.get("check");
		}
		if(config.get("roleUrls") != null){
			List rus = (List) config.get("roleUrls");
			if(rus != null && rus.size()!=0){
				roleUrls = new ArrayList<RoleUrl>();
				for(int i=0;i<rus.size();i++){
					Map<String,Object> ru = (Map)rus.get(i);
					RoleUrl roleUrl = new RoleUrl();
					roleUrl.setUrl((String)ru.get("url"));
					List<String> ls = new ArrayList<String>();
					List<Object> roles = (List<Object>)ru.get("access");
					if(roles==null || roles.size()==0)
						throw new Exception("url对应的角色不能为空");
					for(Object o: roles){
						ls.add((String)o);
					}
					roleUrl.setAccess(ls);
					roleUrls.add(roleUrl);
				}
			}
		}
	}
	
	public String toString(){
		StringBuffer sb = new StringBuffer();
		sb.append(" index page:").append(index_uri).append("\n login page:").append(login_uri)
		  .append("\n logout page:").append(logout_uri).append("\n check page:").append(check_uri);
		sb.append("\nroleUrls:");
		if(roleUrls == null||roleUrls.size()==0) sb.append("0\n");
		else {
			for(RoleUrl roleUrl: roleUrls){
				sb.append("\n\t").append(roleUrls.indexOf(roleUrl)).append(":")
				.append(roleUrl.toString()).append("\n");
			}
		}
		return sb.toString();
	}
	public static String toLocalString(){
		return new ApplicationConfig().toString();
	}
}
